package upload;

import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class AreaDemo {

    private static Connection conn;

    /**
     * @param args
     */
    public static void main(String[] args) {
        conn = getCon();

        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String[] arr = { "1", "2", "3", "4" };
            for (String s : arr) {
                ResultSet rs = conn.createStatement().executeQuery("select * from mi_area where area_lev=" + s);
                List<NameCode> list = new ArrayList<NameCode>();
                while (rs.next()) {
                    NameCode bean = new NameCode();
                    bean.setName(rs.getString(3));
                    bean.setCode(rs.getString(1));
                    list.add(bean);
                }

                StringBuffer sst = new StringBuffer();
                if (s.equals("1")) {
                    map.put("province", list);

                }
                if (s.equals("2")) {
                    map.put("city", list);
                }
                if (s.equals("3")) {
                    map.put("district", list);
                }
                if (s.equals("4")) {
                    map.put("street", list);
                }
            }

            // 保存json
            FileWriter fwriter = new FileWriter(new File("c:/area-3.txt"));
            fwriter.write(JSON.toJSONString(map));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public static Connection getCon() {
        try {
            String driver = "oracle.jdbc.driver.OracleDriver";
            String url = "jdbc:oracle:thin:@192.168.2.73:1521:yymis";
            String user = "yymis";
            String pwd = "yymis";
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, pwd);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
